home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / NewRae-c / Original < prev   
Encoding:
Text File  |  1994-12-04  |  4.5 KB  |  221 lines  |  [TEXT/EDIT]

  1. /* Written 11:22 am  Aug 22, 1986 by sdh@joevax.UUCP in uiucdcsb:net.sources.mac */
  2. /* ---------- "source to rae demo" ---------- */
  3.  
  4. /* rae.c */
  5. /* this is the demo "rae" originally written for blit terminals
  6.  * converted to the Macintosh in Aztec C version 1.06H
  7.  * Steve Hawley 8/22/86
  8.  *
  9.  * I can't vouch for the code for rae. It was copied directly.
  10.  * I only changed the system commands and initialization sequence.
  11.  */
  12. #include <quickdraw.h>
  13. #include <events.h>
  14. #include <osutils.h>
  15.  
  16. #define HEIGHT  16
  17. #define WIDTH   16
  18. #define XMIN    0
  19. #define YMIN    0
  20. #define XMAX    511
  21. #define YMAX    341
  22. #define TOP     YMIN
  23. #define BOT     (YMAX-1)
  24.  
  25. int     GND[128];
  26. int     xpos;
  27.  
  28. GrafPort myPort;
  29.  
  30. /* This is the bits for the face that gets bounced around.
  31.  * the original rae used just circles.
  32.  */
  33. static unsigned char facebits[32] = {
  34.     0x07, 0xe0, 0x18, 0x18, 0x20, 0x04, 0x42, 0x42, 0x42, 0x42,
  35.     0x82, 0x41, 0x80, 0x01, 0x80, 0x01, 0x84, 0x21, 0x88, 0x11,
  36.     0x94, 0x29, 0x43, 0xc2, 0x40, 0x02, 0x20, 0x04, 0x18, 0x18,
  37.     0x07, 0xe0
  38. };
  39.  
  40. struct BitMap face;
  41.  
  42. main ()
  43. {
  44.     Rect myRect, trect;
  45.  
  46.     register i;
  47.     register time;
  48.     int x, xx, y, yy, xvel, yvel, xacc, yacc, ypos, offset;
  49.     long waste;
  50.  
  51.  
  52.     InitGraf(&thePort);
  53.     OpenPort(&myPort); /* create my own port so I don't need */
  54.                    /* windows and can blacken the whole screen */
  55.     
  56.     HideCursor();
  57.     SetRect(&myRect, 0, 0, XMAX+1, YMAX+1);
  58.     PenPat(black);
  59.     PaintRect(&myRect); /* clear screen to black */
  60.     
  61.     face.baseAddr = &facebits[0]; /* set up bitmap parameters */
  62.     face.rowBytes = 2;
  63.     SetRect(&face.bounds, 0, 0, 16, 16);
  64.     
  65. Again:
  66.     PenPat(white);
  67.     MoveTo(XMIN, YMIN); 
  68.     LineTo(XMIN, YMAX);
  69.     MoveTo(XMIN, YMAX); 
  70.     LineTo(XMAX, YMAX);
  71.     MoveTo(XMAX, YMAX);
  72.     LineTo(XMAX, YMIN);
  73.     PenPat(black);
  74.  
  75.     for (i=0; i<128; i++) {
  76.         x = i<<3;
  77.         if (x <= XMIN+8 || x >= XMAX-8)
  78.             GND[i] = 0;
  79.         else {
  80.             GND[i] = BOT - 9;
  81.             if (i&01)
  82.                 if (GND[i-1]==0)
  83.                     GND[i] = 0;
  84.                 else
  85.                     GND[i] -= 7;
  86.         }
  87.     }
  88.     while (!Button()) { /* was a readkey command */
  89.         xpos = Random() & 0177;
  90.         if (GND[xpos] <= TOP) {
  91.             Delay(1L, &waste); /* was a sleep() call. Waits 1/60 second */
  92.             continue;
  93.         }
  94.         x = xpos << 3;
  95.         xvel = 4 - ((Random()|01)&07);
  96.         yacc = 1;
  97.         yvel = 0;
  98.         y = TOP;
  99.         for (time = 0;; time++) {
  100.             if (Button()) /* was a read key command */
  101.                 return;
  102.             drawit(x,y);
  103.             xx = x; 
  104.             yy = y;         /* save x and y */
  105.             yvel += yacc;
  106.             y += yvel;
  107.             x += xvel;
  108.             if (y > GND[x>>3]) {    /* bounce? */
  109.                 if (yvel>5) {
  110.                     drawit(xx, yy);
  111.                     drawit(x, y);
  112.                     Delay(1L, &waste);
  113.                     drawit(x, y);
  114.                     drawit(xx, yy);
  115.                 }
  116.                 if (y <= GND[xx>>3]) { /* side collision? */
  117.                     x = xx;
  118.                     xvel = -xvel;
  119.                 } else if (yy <= GND[x>>3]) { /*bottom? */
  120.                     y = yy;
  121.                     yvel = -yvel;
  122.                 } else {    /* corner */
  123.                     x = xx;
  124.                     y = yy;
  125.                     xvel = -xvel;
  126.                     yvel = -yvel;
  127.                 }
  128.                 if ((time & 017)==0)
  129.                     xvel = decay(xvel);
  130.                 if (xvel == 0) {
  131.                     xpos = x>>3;
  132.                     if (GND[xpos-1]<GND[xpos]
  133.                      && GND[xpos]<GND[xpos+1])
  134.                         xvel = 1;
  135.                     /* roll left */
  136.                     else if (GND[xpos-1]>GND[xpos]
  137.                           && GND[xpos]>GND[xpos+1])
  138.                         xvel = -1;
  139.                     /* on hilltop */
  140.                     else if (GND[xpos-1]>GND[xpos]
  141.                           && GND[xpos]<GND[xpos+1]) {
  142.                         if (Random() & 01)
  143.                             xvel = 1;
  144.                         else
  145.                             xvel = -1;
  146.                     }
  147.                 }
  148.                 yvel = decay(yvel);
  149.             }
  150.             Delay(1L, &waste);
  151.             drawit(xx,yy);
  152.             if (xvel==0 && yvel==0 && y > GND[x>>3]-4)
  153.                 break;
  154.         }
  155.         for (;;) {
  156.             /* find stable position */
  157.             if (GND[xpos-1]<GND[xpos] && GND[xpos]>GND[xpos+1]) {
  158.                 drawit(xpos<<3, GND[xpos]);
  159.                 GND[xpos] -= 21;
  160.                 if (GND[xpos-1]<=0)
  161.                     GND[xpos] -= 7;
  162.                 GND[xpos+1] -= 7;
  163.                 break;
  164.             }
  165.             /* roll right */
  166.             if (GND[xpos-1]<GND[xpos] && GND[xpos]<GND[xpos+1]) {
  167.                 xpos++;
  168.                 continue;
  169.             }
  170.             /* roll left */
  171.             if (GND[xpos-1]>GND[xpos] && GND[xpos]>GND[xpos+1]) {
  172.                 xpos--;
  173.                 continue;
  174.             }
  175.             /* on hilltop, choose at Random */
  176.             if (GND[xpos-1]>GND[xpos] && GND[xpos]<GND[xpos+1]) {
  177.                 if (Random() & 01)
  178.                     xpos++;
  179.                 else
  180.                     xpos--;
  181.                 continue;
  182.             }
  183.             /* else botch */
  184.             drawit(xpos<<3, GND[xpos]);
  185.             PaintRect(&myRect); /* was a call to f_rect() */
  186.             break;
  187.         }
  188.     }
  189. }
  190.  
  191. drawit(x,y)
  192. int x,y;
  193. {       
  194.     int i;
  195.     Rect trect;
  196.  
  197.     if (x<XMIN)
  198.         x = XMIN+8;
  199.     if (y<TOP)
  200.         y = TOP+8;
  201.     if (x>XMAX)
  202.         x = XMAX-8;
  203.     xpos = x>>3;
  204.     if (y>GND[xpos])
  205.         y = GND[xpos];
  206.     /* was a call to bitblt() */
  207.     SetRect(&trect, x-12, y-8, x+4, y+8);
  208.     CopyBits(&face, &myPort.portBits, &face.bounds, &trect, srcXor, 0L);
  209. }
  210.  
  211. decay(v)
  212. register v;
  213. {
  214.     if (v==0)
  215.         return(v);
  216.     if (v > 0)
  217.         return(v-1-(v>>3));
  218.     return(v+1-(v>>3));
  219. }
  220. /* End of text from uiucdcsb:net.sources.mac */
  221.